package hflyeer.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import hflyeer.dao.SaleChanceMapper;
import hflyeer.pojo.entity.SaleChance;
import hflyeer.pojo.query.SaleChanceQuery;
import hflyeer.service.SaleChanceService;
import hflyeer.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * SaleChanceServiceImpl
 *
 * @author hflyeer
 * @date 2021-06-10
 * @describe
 */
@Service
public class SaleChanceServiceImpl implements SaleChanceService {

    @Resource
    private SaleChanceMapper saleChanceMapper;

    @Override
    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery query) {

        Map<String, Object> dataMap = new HashMap<>();

        PageHelper.startPage(query.getPage(),query.getLimit());
        PageInfo<SaleChance> saleChancePageInfo = new PageInfo<>(saleChanceMapper.querySaleChanceByParams(query));

        dataMap.put("code",0);
        dataMap.put("msg","success");
        dataMap.put("count",saleChancePageInfo.getTotal());
        dataMap.put("data",saleChancePageInfo.getList());

        return dataMap;
    }

    @Transactional
    @Override
    public Map<String,Object> addSaleChance(SaleChance saleChance) {
        Map<String, Object> dataMap = new HashMap<>();

        saleChance.setCreateDate(new Date());
        /**
         * 格式化分配状态
         * 0-未分配
         * 1-已分配
         * @param d
         */
        if (StringUtils.isBlank(saleChance.getAssignMan())){
            saleChance.setState(0);
            saleChance.setAssignTime(new Date());
        }else {
            saleChance.setState(1);
        }
        saleChance.setIsValid(1); // 设置是否有效
        /**
         * 格式化开发状态
         * 0-未开发
         * 1-开发中
         * 2-开发成功
         * 3-开发失败
         * @param devResult
         * @returns {string}
         */
        saleChance.setDevResult(0); // 设置开发状态
        int insert = saleChanceMapper.insert(saleChance);
        AssertUtil.isTrue(insert<=0,"添加失败");
        dataMap.put("code",200);
        dataMap.put("msg","success");
        dataMap.put("result",saleChance.getId());

        return dataMap;
    }

    @Transactional
    @Override
    public Map<String, Object> deleteSaleChance(Integer[] ids) {
        Map<String, Object> dataMap = new HashMap<>();

        Integer integer = saleChanceMapper.deleteBatch(ids);
        AssertUtil.isTrue(integer<=0,"删除失败");


        dataMap.put("code",200);
        dataMap.put("msg","success");
        dataMap.put("result","删除成功");
        return dataMap;
    }

    @Override
    public Map<String, Object> updateSaleChance(SaleChance saleChance) {
        Map<String, Object> dataMap = new HashMap<>();
        saleChance.setUpdateDate(new Date());
        /**
         * 格式化分配状态
         * 0-未分配
         * 1-已分配
         * @param d
         */
        if (!StringUtils.isBlank(saleChance.getAssignMan())){
            saleChance.setState(1);
            saleChance.setAssignTime(new Date());
        }
        int i = saleChanceMapper.updateByPrimaryKeySelective(saleChance);
        AssertUtil.isTrue(i<=0,"更新失败");

        dataMap.put("code",200);
        dataMap.put("msg","success");
        dataMap.put("result","更新成功");
        return dataMap;
    }
}
